#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int myfunc(int a, int b)
{
    int c;
    char x[50] = "xiao";

    c = a + b;
    strncpy(&x[0], "as", 2);

    printf("%x== %s== \n", c, &x[0]);  // ddd== asao==
    return c;
}

int main()
{
    int a = 0x666;
    int b = 0x777;
    int c[1];

    c[0] = myfunc(a, b);
    printf("%x\n", &c[0]); // 5d2b2e40 

	return 0;
}

#if 0

/*
 * intel
 */
0000000000001169 <myfunc>:
    1169:	f3 0f 1e fa          	endbr64 
    116d:	55                   	push   %rbp
    116e:	48 89 e5             	mov    %rsp,%rbp
    1171:	48 83 ec 60          	sub    $0x60,%rsp		// rsp-=0x60
    1175:	89 7d ac             	mov    %edi,-0x54(%rbp)		// (rpb-0x54)=edi
    1178:	89 75 a8             	mov    %esi,-0x58(%rbp)		// (rbp-0x58)=esi
    117b:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    1182:	00 00 
    1184:	48 89 45 f8          	mov    %rax,-0x8(%rbp)		// (rbp-0x8)=rax
    1188:	31 c0                	xor    %eax,%eax		// eax=0
    118a:	48 c7 45 c0 78 69 61 	movq   $0x6f616978,-0x40(%rbp)	// (rbp-0x40)=0x6f616978
    1191:	6f 
    1192:	48 c7 45 c8 00 00 00 	movq   $0x0,-0x38(%rbp)		// (rbp-0x38)=0
    1199:	00 
    119a:	48 c7 45 d0 00 00 00 	movq   $0x0,-0x30(%rbp)		// (rbp-0x30)=0
    11a1:	00 
    11a2:	48 c7 45 d8 00 00 00 	movq   $0x0,-0x28(%rbp)		// (rbp-0x28)=0
    11a9:	00 
    11aa:	48 c7 45 e0 00 00 00 	movq   $0x0,-0x20(%rbp)		// (rbp-0x20)=0
    11b1:	00 
    11b2:	48 c7 45 e8 00 00 00 	movq   $0x0,-0x18(%rbp)		// (rbp-0x18)=0
    11b9:	00 
    11ba:	66 c7 45 f0 00 00    	movw   $0x0,-0x10(%rbp)		// (rbp-0x10)=0
    11c0:	8b 55 ac             	mov    -0x54(%rbp),%edx		// edx=(rbp-0x54)
    11c3:	8b 45 a8             	mov    -0x58(%rbp),%eax		// eax=(rbp-0x58)
    11c6:	01 d0                	add    %edx,%eax		// eax+=edx
    11c8:	89 45 bc             	mov    %eax,-0x44(%rbp)		// (rbp-0x44)=eax
    11cb:	48 8d 45 c0          	lea    -0x40(%rbp),%rax		// rax=(rbp-0x40)
    11cf:	66 c7 00 61 73       	movw   $0x7361,(%rax)		// (rax)=0x7361
    11d4:	48 8d 55 c0          	lea    -0x40(%rbp),%rdx		// rdx=(rbp-0x40)
    11d8:	8b 45 bc             	mov    -0x44(%rbp),%eax		// eax=(rbp-0x44)
    11db:	89 c6                	mov    %eax,%esi		// esi=eax
    11dd:	48 8d 3d 20 0e 00 00 	lea    0xe20(%rip),%rdi        # 2004 <_IO_stdin_used+0x4> rdi=rip+0xe20
    11e4:	b8 00 00 00 00       	mov    $0x0,%eax		// eax=0
    11e9:	e8 82 fe ff ff       	callq  1070 <printf@plt>	// printf(rdi, esi)
    11ee:	8b 45 bc             	mov    -0x44(%rbp),%eax		// eax=(rbp-0x44)
    11f1:	48 8b 4d f8          	mov    -0x8(%rbp),%rcx		// rcx=(rbp-0x8)
    11f5:	64 48 33 0c 25 28 00 	xor    %fs:0x28,%rcx
    11fc:	00 00 
    11fe:	74 05                	je     1205 <myfunc+0x9c>
    1200:	e8 5b fe ff ff       	callq  1060 <__stack_chk_fail@plt>
    1205:	c9                   	leaveq 
    1206:	c3                   	retq   

0000000000001207 <main>:
    1207:	f3 0f 1e fa          	endbr64 
    120b:	55                   	push   %rbp
    120c:	48 89 e5             	mov    %rsp,%rbp
    120f:	48 83 ec 20          	sub    $0x20,%rsp		// rsp-=0x20
    1213:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax		
    121a:	00 00 
    121c:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    1220:	31 c0                	xor    %eax,%eax		// eax=0
    1222:	c7 45 ec 66 06 00 00 	movl   $0x666,-0x14(%rbp)	// (rbp-0x14)=0x666
    1229:	c7 45 f0 77 07 00 00 	movl   $0x777,-0x10(%rbp)	// (rbp-0x10)=0x777
    1230:	8b 55 f0             	mov    -0x10(%rbp),%edx		// edx=(rbp-0x10)
    1233:	8b 45 ec             	mov    -0x14(%rbp),%eax		// eax=(rbp-0x14)
    1236:	89 d6                	mov    %edx,%esi		// esi=edx
    1238:	89 c7                	mov    %eax,%edi		// edi=eax
    123a:	e8 2a ff ff ff       	callq  1169 <myfunc>		// myfunc(edi, esi)
    123f:	89 45 f4             	mov    %eax,-0xc(%rbp)		// (rbp-0xc)=eax
    1242:	48 8d 45 f4          	lea    -0xc(%rbp),%rax		// rax=(rbp-0xc)
    1246:	48 89 c6             	mov    %rax,%rsi		// rsi=rax
    1249:	48 8d 3d c0 0d 00 00 	lea    0xdc0(%rip),%rdi        # 2010 <_IO_stdin_used+0x10> rdi=rip+0xdc0
    1250:	b8 00 00 00 00       	mov    $0x0,%eax		// eax=0
    1255:	e8 16 fe ff ff       	callq  1070 <printf@plt>	// printf(rdi, rsi)
    125a:	b8 00 00 00 00       	mov    $0x0,%eax		// eax=0
    125f:	48 8b 4d f8          	mov    -0x8(%rbp),%rcx		// rcx=(rbp-0x8)
    1263:	64 48 33 0c 25 28 00 	xor    %fs:0x28,%rcx
    126a:	00 00 
    126c:	74 05                	je     1273 <main+0x6c>
    126e:	e8 ed fd ff ff       	callq  1060 <__stack_chk_fail@plt>
    1273:	c9                   	leaveq 
    1274:	c3                   	retq   
    1275:	66 2e 0f 1f 84 00 00 	nopw   %cs:0x0(%rax,%rax,1)
    127c:	00 00 00 
    127f:	90                   	nop

/*
 * arm
 */
000000000040055c <myfunc>:
  40055c:	a9ba7bfd 	stp	x29, x30, [sp, #-96]!
  400560:	910003fd 	mov	x29, sp
  400564:	b9001fa0 	str	w0, [x29, #28]		// [x29+28]=w0
  400568:	b9001ba1 	str	w1, [x29, #24]		// [x29+24]=w1
  40056c:	90000000 	adrp	x0, 400000 <_init-0x3e8>
  400570:	911c0001 	add	x1, x0, #0x700		// x1=400700
  400574:	9100a3a0 	add	x0, x29, #0x28		// x0=x29+0x28
  400578:	b9400022 	ldr	w2, [x1]		// w2=[x1]
  40057c:	b9000002 	str	w2, [x0]		// [x0]=w2
  400580:	b8401021 	ldur	w1, [x1, #1]		// w1=[x1+1]
  400584:	b8001001 	stur	w1, [x0, #1]		// [x0+1]=w1
  400588:	aa1d03e0 	mov	x0, x29			// x0=x29
  40058c:	9100b400 	add	x0, x0, #0x2d		// x0=x0+0x2d
  400590:	a9007c1f 	stp	xzr, xzr, [x0]		// [x0]=0, [x0+0x8]=0
  400594:	aa1d03e0 	mov	x0, x29			// x0=x29
  400598:	9100f400 	add	x0, x0, #0x3d		// x0=0x29+0x3d
  40059c:	a9007c1f 	stp	xzr, xzr, [x0]		// [x0]=0, [x0+0x8]=0
  4005a0:	f804d3bf 	stur	xzr, [x29, #77]		// [x29+77]=0
  4005a4:	b80553bf 	stur	wzr, [x29, #85]		// [x29+85]=0
  4005a8:	390167bf 	strb	wzr, [x29, #89]		// [x29+89]=0
  4005ac:	b9401fa1 	ldr	w1, [x29, #28]		// w1=[x29+89]
  4005b0:	b9401ba0 	ldr	w0, [x29, #24]		// w0=[x29+24]
  4005b4:	0b000020 	add	w0, w1, w0		// w0=w1+w0
  4005b8:	b9005fa0 	str	w0, [x29, #92]		// [x29+92]=w0
  4005bc:	9100a3a0 	add	x0, x29, #0x28		// x0=x29+0x28
  4005c0:	90000001 	adrp	x1, 400000 <_init-0x3e8>
  4005c4:	911ba021 	add	x1, x1, #0x6e8		// x1=4006e8
  4005c8:	79400021 	ldrh	w1, [x1]		// w1=[x1]
  4005cc:	79000001 	strh	w1, [x0]		// [x0]=w1
  4005d0:	9100a3a1 	add	x1, x29, #0x28		// x1=x29+0x28
  4005d4:	90000000 	adrp	x0, 400000 <_init-0x3e8>
  4005d8:	911bc000 	add	x0, x0, #0x6f0		// x0=4006f0
  4005dc:	aa0103e2 	mov	x2, x1			// x2=x1
  4005e0:	b9405fa1 	ldr	w1, [x29, #92]		// w1=[x29+92]
  4005e4:	97ffff9b 	bl	400450 <printf@plt>	// printf(x0, w1, x2)
  4005e8:	b9405fa0 	ldr	w0, [x29, #92]		// w0=[x29+92]
  4005ec:	a8c67bfd 	ldp	x29, x30, [sp], #96
  4005f0:	d65f03c0 	ret

00000000004005f4 <main>:
  4005f4:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  4005f8:	910003fd 	mov	x29, sp
  4005fc:	5280ccc0 	mov	w0, #0x666                 	// #1638
  400600:	b9001fa0 	str	w0, [x29, #28]		// [x29+28]=0x666
  400604:	5280eee0 	mov	w0, #0x777                 	// #1911
  400608:	b9001ba0 	str	w0, [x29, #24]		// [x29+24]=0x777
  40060c:	b9401ba1 	ldr	w1, [x29, #24]		// w1=0x777
  400610:	b9401fa0 	ldr	w0, [x29, #28]		// w0=0x666
  400614:	97ffffd2 	bl	40055c <myfunc>		// myfunc(0x666, 0x777)
  400618:	b90013a0 	str	w0, [x29, #16]		// [x29+16]=w0
  40061c:	910043a1 	add	x1, x29, #0x10		// x1=x29+0x10
  400620:	90000000 	adrp	x0, 400000 <_init-0x3e8>
  400624:	911ce000 	add	x0, x0, #0x738		// x0=400738
  400628:	97ffff8a 	bl	400450 <printf@plt>	// printf(x0, x1)
  40062c:	52800000 	mov	w0, #0x0                   	// #0
  400630:	a8c27bfd 	ldp	x29, x30, [sp], #32
  400634:	d65f03c0 	ret

/*
 *
 */
int myfunc(int a, int b)
{
   0:	e92d4800 	push	{fp, lr}
   4:	e28db004 	add	fp, sp, #4
   8:	e24dd040 	sub	sp, sp, #64	; 0x40
   c:	e50b0040 	str	r0, [fp, #-64]	; 0xffffffc0    a
  10:	e50b1044 	str	r1, [fp, #-68]	; 0xffffffbc    b
    int c;
    char x[50] = "xiao";
  14:	e59f2064 	ldr	r2, [pc, #100]	; 80 <myfunc+0x80>
  18:	e24b303c 	sub	r3, fp, #60	; 0x3c  x
  1c:	e8920003 	ldm	r2, {r0, r1} ; r0= 80 address , r1 = 80+4 address
  20:	e5830000 	str	r0, [r3]     ; r0 ---> fp-60 
  24:	e2833004 	add	r3, r3, #4   ; r3 = fp-60+4 = fp - 56
  28:	e5c31000 	strb	r1, [r3] ; r1 ---> fp - 56
  2c:	e24b3037 	sub	r3, fp, #55	; 0x37 r3 = fp - 55
  30:	e3a0202d 	mov	r2, #45	; 0x2d   len=45, 
 x  34:	e3a01000 	mov	r1, #0  ; 0
  38:	e1a00003 	mov	r0, r3  ; x[5], 除xiao外，剩下的空间由memset负责初始化为0
  3c:	ebfffffe 	bl	0 <memset>

    c = a + b;
  40:	e51b2040 	ldr	r2, [fp, #-64]	; 0xffffffc0    a
  44:	e51b3044 	ldr	r3, [fp, #-68]	; 0xffffffbc    b
  48:	e0823003 	add	r3, r2, r3
  4c:	e50b3008 	str	r3, [fp, #-8] ; c
    strncpy(&x[0], "as", 2);
  50:	e24b303c 	sub	r3, fp, #60	; 0x3c  x[0]
  54:	e59f2028 	ldr	r2, [pc, #40]	; 84 <myfunc+0x84>
  58:	e1c320b0 	strh	r2, [r3]    ; write as

    printf("%x== %s== \n", c, &x[0]);  // ddd== asao==
  5c:	e24b303c 	sub	r3, fp, #60	; 0x3c  x[0]
  60:	e1a02003 	mov	r2, r3      ; r2 = x
  64:	e51b1008 	ldr	r1, [fp, #-8] ; c
  68:	e59f0018 	ldr	r0, [pc, #24]	; 88 <myfunc+0x88>
  6c:	ebfffffe 	bl	0 <printf>
    return c;
  70:	e51b3008 	ldr	r3, [fp, #-8] ; c
}

/*
 *
 */
int myfunc(int a, int b)
{
   0:	a9ba7bfd 	stp	x29, x30, [sp, #-96]!
   4:	910003fd 	mov	x29, sp
   8:	b9001fa0 	str	w0, [x29, #28] ; a
   c:	b9001ba1 	str	w1, [x29, #24] ; b
    int c;
    char x[50] = "xiao";
  10:	90000000 	adrp	x0, 0 <myfunc> ; base address
  14:	91000001 	add	x1, x0, #0x0       ; var offset
  18:	9100a3a0 	add	x0, x29, #0x28     ; x
  1c:	b9400022 	ldr	w2, [x1]           ; w2=[x1]=xi
  20:	b9000002 	str	w2, [x0]           ; x[0]=w2=xi
  24:	b8401021 	ldur	w1, [x1, #1]   ; w1=[x1+1]=ao
  28:	b8001001 	stur	w1, [x0, #1]   ; [x0+1]=w1=ao
  2c:	9100b7a0 	add	x0, x29, #0x2d     ; x0=x29+0x2d  45
  30:	a9007c1f 	stp	xzr, xzr, [x0]     ; [x0]=0
  34:	9100f7a0 	add	x0, x29, #0x3d     ; 61
  38:	a9007c1f 	stp	xzr, xzr, [x0]     ; 0
  3c:	f804d3bf 	stur	xzr, [x29, #77] ; 64bit 0
  40:	b80553bf 	stur	wzr, [x29, #85] ; 32bit 0
  44:	390167bf 	strb	wzr, [x29, #89] ; 32bit 0

    c = a + b;
  48:	b9401fa1 	ldr	w1, [x29, #28] ; a
  4c:	b9401ba0 	ldr	w0, [x29, #24] ; b
  50:	0b000020 	add	w0, w1, w0 
  54:	b9005fa0 	str	w0, [x29, #92] ; c
    strncpy(&x[0], "as", 2);
  58:	9100a3a0 	add	x0, x29, #0x28 ; x
  5c:	90000001 	adrp	x1, 0 <myfunc> ; as base address
  60:	91000021 	add	x1, x1, #0x0       ; as
  64:	79400021 	ldrh	w1, [x1]       ; w1=as
  68:	79000001 	strh	w1, [x0]       ; x[0]=as

    printf("%x== %s== \n", c, &x[0]);  // ddd== asao==
  6c:	9100a3a1 	add	x1, x29, #0x28 ; x
  70:	90000000 	adrp	x0, 0 <myfunc>
  74:	91000000 	add	x0, x0, #0x0 ; parameter address
  78:	aa0103e2 	mov	x2, x1       ; x2 = x
  7c:	b9405fa1 	ldr	w1, [x29, #92] ; c
  80:	94000000 	bl	0 <printf>
    return c;
  84:	b9405fa0 	ldr	w0, [x29, #92]
}
  88:	a8c67bfd 	ldp	x29, x30, [sp], #96
  8c:	d65f03c0 	ret

0000000000000090 <main>:

void main()
{
  90:	a9be7bfd 	stp	x29, x30, [sp, #-32]! ; save fp and lr register
  94:	910003fd 	mov	x29, sp               ; fp = sp
    int a = 0x666;
  98:	5280ccc0 	mov	w0, #0x666                 	// #1638
  9c:	b9001fa0 	str	w0, [x29, #28]              ; a
    int b = 0x777;
  a0:	5280eee0 	mov	w0, #0x777                 	// #1911
  a4:	b9001ba0 	str	w0, [x29, #24]              ; b
    int c[1];

    c[0] = myfunc(a, b);
  a8:	b9401ba1 	ldr	w1, [x29, #24] ; b
  ac:	b9401fa0 	ldr	w0, [x29, #28] ; a
  b0:	94000000 	bl	0 <myfunc>
  b4:	b90013a0 	str	w0, [x29, #16] ; c[0]
    printf("%x\n", &c[0]); // 5d2b2e40 
  b8:	910043a1 	add	x1, x29, #0x10 ; c[0]
  bc:	90000000 	adrp	x0, 0 <myfunc>
  c0:	91000000 	add	x0, x0, #0x0 ; parameter address
  c4:	94000000 	bl	0 <printf>
}



#endif
